//
//  MYStatusToolBar.m
//  奇闻
//
//  Created by 似水灵修 on 14-11-28.
//  Copyright (c) 2014年 MingYueGuCheng. All rights reserved.
//  工具条

#import "MYStatusToolBar.h"
#import "MYStatus.h"

@interface MYStatusToolBar ()
/**
 *  按钮数组（UButton）
 */
@property (nonatomic, strong) NSMutableArray *btns;
/**
 *  分割线数组（UIImageView）
 */
@property (nonatomic, strong) NSMutableArray *divides;
/**
 *  转发
 */
@property (nonatomic, weak) UIButton *retweetBtn;
/**
 *  评论
 */
@property (nonatomic, weak) UIButton *commentBtn;
/**
 *  赞
 */
@property (nonatomic, weak) UIButton *unlikeBtn;
@end
@implementation MYStatusToolBar
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //添加所有子控件
        [self setupAllChildView];
        self.image = [UIImage imageNamed:@"timeline_card_bottom_background"];
        self.userInteractionEnabled = YES;
    }
    return self;
}
- (NSMutableArray *)btns
{
    if (nil == _btns) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}
- (NSMutableArray *)divides
{
    if (nil == _divides) {
        _divides = [NSMutableArray array];
    }
    return _divides;
}
//添加所有子控件
- (void)setupAllChildView
{
    // 转发
    self.retweetBtn = [self addOneChildViewWithTitle:@"转发" image:[UIImage imageNamed:@"timeline_icon_retweet"]];
    // 评论
    self.commentBtn = [self addOneChildViewWithTitle:@"评论" image:[UIImage imageNamed:@"timeline_icon_comment"]];
    //赞
    self.unlikeBtn = [self addOneChildViewWithTitle:@"赞" image:[UIImage imageNamed:@"timeline_icon_unlike"]];
    //添加分割线
    for (int i = 0; i < 2; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"timeline_card_bottom_line"]];
        [self addSubview:imageView];
        [self.divides addObject:imageView];
    }
}
//添加一个子控件
- (UIButton *)addOneChildViewWithTitle:(NSString *)title image:(UIImage *)image
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
     // left->相对于之前title左边的位置添加10的间距
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [btn setBackgroundImage:[UIImage imageNamed:@"timeline_card_bottom_background"] forState:UIControlStateNormal];
//    [btn setBackgroundImage:[UIImage imageNamed:@"timeline_card_bottom_background_highlighted"] forState:UIControlStateNormal];
    [self addSubview:btn];
    [self.btns addObject:btn];
    return btn;
}
- (void)setStatus:(MYStatus *)status
{
    // count > 10000
    // 9999 9999
    // 12000 1.2W
    // 10020 1W
    // 10020 /10000.0 1.0W -> 1W @".0" @""
    _status = status;
    //转发
    [self setupButton:self.retweetBtn count:status.reposts_count originalTitle:@"转发"];
    //评论
    [self setupButton:self.commentBtn count:status.comments_count originalTitle:@"评论"];
    //赞
    [self setupButton:self.unlikeBtn count:status.attitudes_count originalTitle:@"赞"];
}
//设置按钮标题
- (void)setupButton:(UIButton *)btn count:(int)count originalTitle:(NSString *)title
{
    if (count) {// 有木有转发数
        if (count > 10000) {
            CGFloat doubleRetweet = count / 10000.0;
            title = [NSString stringWithFormat:@"%.1fW", doubleRetweet];
            title = [title stringByReplacingOccurrencesOfString:@".0" withString:@""];
        }
        else {  // 小于10000
        title = [NSString stringWithFormat:@"%i", count];
        }
    }
    [btn setTitle:title forState:UIControlStateNormal];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
     // 计算所有的按钮尺寸
    NSUInteger count = self.btns.count;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = self.width / self.btns.count;
    CGFloat h = self.height;
    for (NSUInteger i = 0; i < count; i++ ) {
        UIButton *btn = self.btns[i];
        x = i * w;
        btn.frame = CGRectMake(x, y, w, h);
    }
    // 计算所有分割线的尺寸
    for (int i = 0; i < self.divides.count; i++) {
        UIImageView *imageView = self.divides[i];
        UIButton *btn = self.btns[i + 1];
        imageView.x = btn.x;
    }
}
@end
